/**
 * Created by fire on 15-10-9.
 */
var page_json;
var paginator = '';
var url = '';
var append = '?';
//分页器渲染
var disabled_text = function (text) {
        return '<li class="disabled"><span>' + text + '</span></li>';
    },
    page_link = function (url, page, rel) {
        rel = rel ? ' rel="' + rel + '"' : '';
        return '<li><a href=\"javascript:fetchData(' + '\'' + url + '\'' + ');" >' + page + '</a></li>';
    },
    active_page = function (text) {
        return '<li class="active"><span>' + text + '</span></li>';
    };

var previous = function () {
        if (page_json.current_page == '1') {
            return disabled_text('&laquo;');
        } else {
            var pre_page = parseInt(page_json.current_page) - 1;
            return page_link(url + append + 'p=' + pre_page, '&laquo;', '上一页');
        }
    },
    next = function () {
        if (page_json.current_page == page_json.last_page) {
            return disabled_text('&raquo;');
        } else {
            var next_page = parseInt(page_json.current_page) + 1;
            return page_link(url + append + 'p=' + next_page, '&raquo;', '上一页');
        }
    },
    dots = function () {
        return disabled_text('...');
    };

var page_range = function (from, to) {
        var paginator_inner = '';
        for (var i = from; i <= to; i++) {
            if (i == page_json.current_page) {
                paginator_inner += active_page(page_json.current_page);
            } else {
                paginator_inner += page_link(url + append + 'p=' + i, i);
            }
        }
        return paginator_inner;
    },
    get_start = function () {
        return page_range(1, 2) + dots();
    },
    get_finish = function () {
        return dots() + page_range(parseInt(page_json.last_page) - 1, page_json.last_page);
    };

var make_page =function(data,pagination,append,prev_item){
    if(prev_item == undefined || prev_item<0 || prev_item>data.last_page){
        prev_item = 8;
    }
    //if(last_item== undefined ||last_item<0 || last_item>data.last_page){
    //    last_item = 2;
    //}
    prev_change = (prev_item-2)>0?(prev_item-2):1;
    paginator = "";
    page_json = data;
    append = '?';

    if(page_json.per_page >= page_json.total){
        $(pagination).html(previous() + page_range(1,1) + next());
        return;
    }


    var spilt = url.lastIndexOf(append+'p=');
    if (spilt > 0) {
        url = url.substring(0, url.lastIndexOf(append));
    }

    if (page_json.last_page <= 10) {
        paginator += page_range(1, page_json.last_page);
    } else {
        if (page_json.current_page <= prev_change) {
            paginator += page_range(1, prev_item) + get_finish();
        } else if (page_json.current_page >= parseInt(page_json.last_page) - prev_change) {
            paginator += get_start() + page_range(parseInt(page_json.last_page) - prev_item, page_json.last_page);
        } else {
            if(page_json.current_page-prev_change<2){
                paginator += page_range(1, page_json.current_page+1) + get_finish();
            }else {

                paginator += get_start() + page_range(parseInt(page_json.current_page) - 1, parseInt(page_json.current_page) + 1) + get_finish();
            }
        }
    }
    $(pagination).html(previous() + paginator + next());
};
